package com.sh.sales.happysales.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sh.polymedicine.happysale.result.SimpleResult;
import com.sh.polymedicine.happysale.result.UserInfoResult;
import com.sh.polymedicine.happysale.service.envelop.HappySaleManager;
import com.sh.sales.happysales.R;
import com.sh.sales.happysales.app.AppData;
import com.sh.sales.happysales.app.utils.LoginOutUtil;
import com.sh.sales.happysales.base.BaseActivity;
import com.sh.sales.happysales.datautils.LocalUserInfoUtils;
import com.sh.sales.happysales.service.LoginObsever;
import com.sh.sales.happysales.view.NewToast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by zhouyucheng on 2017/9/25.
 */

public class ChangeNickActivity extends BaseActivity implements LoginObsever {
    @BindView(R.id.set_nickback)
    ImageView setNickback;
    @BindView(R.id.profect_next)
    TextView profectNext;
    @BindView(R.id.current_nick)
    EditText currentNick;
    @BindView(R.id.nick_cancle)
    ImageView nickcancle;
    private Subscriber<SimpleResult> changeNickSubscriber;
    private UserInfoResult mUserInfoResult;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changenick);
        mAppManager.addActivity(this);
        mLoginObserable.registerObserver(this);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        mUserInfoResult = AppData.getInstence().getmUserInfoResult();
        if (mUserInfoResult.getNickname() != null) {
            currentNick.setText(mUserInfoResult.getNickname());
        }
    }

    @OnClick({R.id.set_nickback, R.id.profect_next, R.id.nick_cancle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.set_nickback:
                backInfo(mUserInfoResult);
                mAppManager.finishActivity(this);
                break;
            case R.id.profect_next:
                saveNewNick(currentNick.getText().toString());
                break;
            case R.id.nick_cancle:
                currentNick.setText("");
                break;
        }
    }

    private void saveNewNick(final String nick) {
        String userSign = AppData.getInstence().getmUserToken().getUserSign();
        if (nick.isEmpty()) {
            NewToast.makeText(ChangeNickActivity.this, "新的昵称不能为空");
            return;
        }
        if (changeNickSubscriber != null) {
            if (changeNickSubscriber.isUnsubscribed()) {
                changeNickSubscriber.unsubscribe();
            }
        }
        changeNickSubscriber = new Subscriber<SimpleResult>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.i("CaptureActivity", "onError: " + e);
                if (HappySaleManager.DATA_FAIL.equals(e.getMessage())){
                    String message =e.getCause().getCause().getMessage();
                    NewToast.makeText(ChangeNickActivity.this, message);
                    int ret =Integer.valueOf( e.getCause().getMessage()) ;
                    if (ret>=100&&ret<200){
                        LoginOutUtil.logOut(ChangeNickActivity.this);
                    }
                }else if (HappySaleManager.DATA_NULL.equals(e.getMessage())){
                    NewToast.makeText(ChangeNickActivity.this, e.getCause().getMessage());
                }else{
                    NewToast.makeText(ChangeNickActivity.this, "昵称修改失败,请检查网络");
                }

            }

            @Override
            public void onNext(SimpleResult result) {
                mUserInfoResult.setNickname(nick);
                backInfo(mUserInfoResult);
                LocalUserInfoUtils.updataUserInfoResult(mUserInfoResult, mUserInfoResult.getInfoId());
                finish();
            }
        };
        mHappySaleManager.changeNick(nick, userSign, changeNickSubscriber).subscribe(changeNickSubscriber);
    }

    private void backInfo(UserInfoResult mUserInfoResult) {
        Intent intent = new Intent();
        intent.putExtra("nickName", mUserInfoResult.getNickname());
        ChangeNickActivity.this.setResult(5, intent);
    }

    @Override
    public void refreshUserSign(int Position) {
        saveNewNick(currentNick.getText().toString());
    }

    @Override
    protected void onDestroy() {
        mLoginObserable.unregisterObserver(this);
        super.onDestroy();
    }
}
